package cn.cangfei.massage.service.impl;

import cn.cangfei.massage.dto.param.MassageCmsParam;
import cn.cangfei.massage.entity.MassageCms;
import cn.cangfei.massage.mapper.MassageCmsMapper;
import cn.cangfei.massage.service.MassageCmsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MassageCmsServiceImpl extends ServiceImpl<MassageCmsMapper, MassageCms> implements MassageCmsService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 添加轮播图信息
     *
     * @param param
     * @return
     */
    @Override
    public boolean insert(MassageCmsParam param) {
        MassageCms massageCms = new MassageCms();
        BeanUtils.copyProperties(param, massageCms);
        return save(massageCms);
    }

    /**
     * 轮播图是否存在
     *
     * @param id
     * @return
     */
    @Override
    public boolean countCms(Long id) {
        MassageCms massageCms = getById(id);
        return massageCms != null;
    }

    /**
     * 获取轮播图信息
     *
     * @param id
     * @return
     */
    @Override
    public MassageCms getMassageCmsInfo(Long id) {
        MassageCms massageCms = getById(id);

        return massageCms;
    }

    /**
     * 更新轮播图信息
     *
     * @param id
     * @param param
     * @return
     */
    @Override
    public boolean updateCms(Long id, MassageCmsParam param) {
        MassageCms massageCms = new MassageCms();
        BeanUtils.copyProperties(param, massageCms);
        massageCms.setId(id);
        return updateById(massageCms);
    }

    /**
     * 删除轮播图信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteCms(Long id) {
        return removeById(id);
    }

    /**
     * 分页获取轮播图信息
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<MassageCms> listWithQuery(String name, Integer pageNum, Integer pageSize) {
        IPage<MassageCms> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<MassageCms> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isEmpty(name)) {
            lambdaQueryWrapper.like(MassageCms::getName, name);
        }
        lambdaQueryWrapper.orderByAsc(MassageCms::getSort);
        IPage<MassageCms> page1 = page(page, lambdaQueryWrapper);

        return page1;
    }

    @Override
    public List<MassageCms> searchMassageCmsInfoList() {

        List<MassageCms> list = list();

        return list;
    }
}
